package com.qdu.hyy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qdu.hyy.MovieWritable;
import com.qdu.hyy.Writable.Average;
import com.qdu.hyy.Writable.Count;
import com.qdu.hyy.Writable.Highest;
import com.qdu.hyy.Writable.Popularity;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class toJson {
    public static void main(String[] args) throws IOException {
        List<Popularity> count = new ArrayList<>();

        // 假设 part-r-00001 文件位于本地文件系统中
        String filePath = "output/job4/part-r-00000";

        try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] string = line.split("\t+");
                count.add(new Popularity(string[0],string[1],string[2])); // 假设构造函数存在
            }
        }

        String jsonString = JSON.toJSONString(count, SerializerFeature.PrettyFormat);
        try (FileWriter fileWriter = new FileWriter("C:\\Users\\hanyiyang\\Desktop\\map-reduce-project\\src\\main\\webapp\\word\\job4.json")) {
            // 将JSON字符串写入文件
            fileWriter.write(jsonString);
            fileWriter.flush();
            System.out.println("airlineWritable objects have been written to 'test.json'");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}